package com.healthyeating.manage.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.healthyeating.manage.domain.ActivityUser;
import com.healthyeating.manage.domain.AssocNutrition;
import com.healthyeating.manage.domain.TakePart;
import com.healthyeating.manage.domain.vo.ActivityUserVo;
import com.healthyeating.manage.domain.vo.AssocNutritionVo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.healthyeating.common.annotation.Log;
import com.healthyeating.common.core.controller.BaseController;
import com.healthyeating.common.core.domain.AjaxResult;
import com.healthyeating.common.enums.BusinessType;
import com.healthyeating.manage.domain.Activity;
import com.healthyeating.manage.service.IActivityService;
import com.healthyeating.common.utils.poi.ExcelUtil;
import com.healthyeating.common.core.page.TableDataInfo;

/**
 * 活动Controller
 *
 * @author 段奇乐
 * @date 2025-04-12
 */
@RestController
@RequestMapping("/manage/activity")
public class ActivityController extends BaseController {

    @Autowired
    private IActivityService activityService;

    /**
     * 查询活动列表
     */
    @PreAuthorize("@ss.hasPermi('manage:activity:list')")
    @GetMapping("/list")
    public TableDataInfo list(Activity activity) {
        startPage();
        List<Activity> list = activityService.selectActivityList(activity);
        return getDataTable(list);
    }

    /**
     * 导出活动列表
     */
    @PreAuthorize("@ss.hasPermi('manage:activity:export')")
    @Log(title = "活动", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Activity activity) {
        List<Activity> list = activityService.selectActivityList(activity);
        ExcelUtil<Activity> util = new ExcelUtil<Activity>(Activity.class);
        util.exportExcel(response, list, "活动数据");
    }

    /**
     * 获取活动详细信息
     */
    @PreAuthorize("@ss.hasPermi('manage:activity:query')")
    @GetMapping(value = "/{activityId}")
    public AjaxResult getInfo(@PathVariable("activityId") Long activityId) {
        return success(activityService.selectActivityByActivityId(activityId));
    }

    /**
     * 新增活动
     */
    @PreAuthorize("@ss.hasPermi('manage:activity:add')")
    @Log(title = "活动", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Activity activity) {
        return toAjax(activityService.insertActivity(activity));
    }

    /**
     * 修改活动
     */
    @PreAuthorize("@ss.hasPermi('manage:activity:edit')")
    @Log(title = "活动", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Activity activity) {
        return toAjax(activityService.updateActivity(activity));
    }

    /**
     * 删除活动
     */
    @PreAuthorize("@ss.hasPermi('manage:activity:remove')")
    @Log(title = "活动", businessType = BusinessType.DELETE)
    @DeleteMapping("/{activityIds}")
    public AjaxResult remove(@PathVariable Long[] activityIds) {
        return toAjax(activityService.deleteActivityByActivityIds(activityIds));
    }

    /**
     * 查询参与活动记录列表
     */
    @PreAuthorize("@ss.hasPermi('manage:activity:list')")
    @GetMapping("/getActivityUserList")
    public TableDataInfo getActivityUserList(ActivityUser activityUser) {
        if (activityUser.getActivityId() == null) {
            startPage();
        }
        Map<String, Object> map = activityService.selectActivityUserList(activityUser);
        List<AssocNutrition> oldList = new ArrayList<>();
        List<AssocNutritionVo> newList = new ArrayList<>();
        if (map.get("oldList") != null) {
            oldList = (List<AssocNutrition>) map.get("oldList");
        }

        if (map.get("newList") != null) {
            newList = (List<AssocNutritionVo>) map.get("newList");
        }
//        List<ActivityUserVo> list = activityService.selectActivityUserList(activityUser);
        return getDataTable(oldList, newList);
    }

    /**
     * 参与活动
     */
    @PreAuthorize("@ss.hasPermi('manage:activity:add')")
    @Log(title = "活动", businessType = BusinessType.INSERT)
    @PostMapping("/takepart")
    public AjaxResult takePartActivity(@RequestBody ActivityUser activityUser) {
        return toAjax(activityService.insertActivityUser(activityUser));
    }

    /**
     * 修改活动
     */
    @PreAuthorize("@ss.hasPermi('manage:activity:edit')")
    @Log(title = "活动", businessType = BusinessType.UPDATE)
    @PutMapping("/clockIn")
    public AjaxResult clockIn(@RequestBody TakePart takenPart) {
        return toAjax(activityService.clockIn(takenPart));
    }

    /**
     * 删除活动
     */
    @PreAuthorize("@ss.hasPermi('manage:activity:remove')")
    @Log(title = "活动", businessType = BusinessType.DELETE)
    @DeleteMapping("/activityUser/{activityUserIds}")
    public AjaxResult removeActivityUser(@PathVariable Long[] activityUserIds) {
        return toAjax(activityService.deleteActivityUserByActivityUserIds(activityUserIds));
    }
}
